﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NHibernate;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;

namespace UHUDAL
{
    public class ActiveRecordHelper
    {
        private const string SESSION_NAME = "UHU";

        //创建Session
        public static ISession CreateSession(Type type)
        {
            ISessionFactoryHolder sessionHolder = ActiveRecordMediator.GetSessionFactoryHolder();
            return sessionHolder.CreateSession(type);
        }

        //获取当前Session
        public static ISession GetSession(Type type)
        {
            ISession session = GetSession();
            if (session == null)
            {
                session = CreateSession(type);
                SetSession(session);
            }
            return session;
        }

        //从当前上下文中获取Session
        public static ISession GetSession()
        {
            return (ISession)HttpContext.Current.Items[SESSION_NAME];
        }

        //把Session存放到当前上下文对象
        public static void SetSession(ISession value)
        {
            if (value != null)
            {
                HttpContext.Current.Items.Add(SESSION_NAME, value);
            }
            else
            {
                HttpContext.Current.Items.Remove(SESSION_NAME);
            }
        }
    }
}
